package javabase.generic;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class GenericTest {
	
	public static void main(String[] args) {
		ArrayList list1 = new ArrayList();
		list1.add(1);
		list1.add(1L);
		list1.add("1");
		
//		int i = (Integer) list1.get(1);
		
		ArrayList<String> list2 = new ArrayList<>();
//		list2.add(1);
//		list2.add(1L);
		list2.add("1");
		
		String s = list2.get(0);
		
		
		try {
			list2.getClass().getMethod("add", String.class).invoke(list2, 123);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		try {
			Constructor<String> constructor = String.class.getConstructor(String.class);
			
			String s2 = constructor.newInstance("123");
			
			
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Method method = GenericTest.class.getMethod("apply", List.class);
			
			Type[] types = method.getGenericParameterTypes();
			ParameterizedType pType = (ParameterizedType) types[0];
			System.out.println(pType.getRawType());
			System.out.println(pType.getActualTypeArguments()[0]);
			
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	
	public static void apply(List<String> list) {
		
	}
	
	private static <T> T autoConvert(Object o) {
		return (T)o;
	}
	
	private static <T> void fillArray(T[] a, T obj) {
		for(int i=0; i<a.length; i++) {
			a[i] = obj;
		}
	}
	
}
